from django import forms
from apps.web.forms.bootstrap import BootStrapForm
from apps.web import models


class IssuesModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_datetime']
        widgets = {
            'assign': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
            'attention': forms.SelectMultiple(
                attrs={'class': 'selectpicker', 'data-live-search': 'true', 'data-actions-box': 'true'}),
            'parent': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 处理数据初始化
        # 获取当前项目的问题类型
        self.fields['issues_type'].choices = models.IssuesType.objects.filter(
            project=request.tracer.project).values_list('id', 'title')

        # 获取当前项目的所有模块
        module_list = [('', '没有选择任何项'), ]
        module_obj = self.fields['module'].choices = models.Module.objects.filter(
            project=request.tracer.project).values_list('id', 'title')
        module_list.extend(module_obj)
        self.fields['module'].choices = module_list

        # 指派和关注者
        total_user_list = [(request.tracer.project.creator_id, request.tracer.project.creator.username), ]
        # 找到当前项目的参与者和创建者
        project_user_list = models.ProjectUser.objects.filter(project=request.tracer.project).values_list('user_id',
                                                                                                          'user__username')
        total_user_list.extend(project_user_list)
        self.fields['assign'].choices = [('', '不指派任何人')] + total_user_list
        self.fields['attention'].choices = [('', '没有选择任何项')] +  total_user_list

        # 父级问题，当前项目的已创建的所有项目
        parent_list = [('', '没有选择任何项'), ]
        parent_obj = models.Issues.objects.filter(project=request.tracer.project).values_list('id', 'subject')
        parent_list.extend(parent_obj)
        self.fields['parent'].choices = parent_list


class IssuesReplyModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.IssuesReply
        fields = ['content', 'reply']


class InviteModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ['period', 'count']
